package org.fund.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.fund.base_service.domain.Position;
import org.fund.base_service.domain.vo.PositionVO;
import org.fund.base_service.domain.vo.TransactionRecordVO;
import org.fund.base_service.factory.OperatorFactory;
import org.fund.base_service.service.PositionService;
import org.fund.common.constant.FundEnum;
import org.fund.common.core.domain.BaseResult;
import org.fund.common.util.FundUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 持仓(Position)表控制层
 *
 * @author makejava
 * @since 2022-03-13 01:28:43
 */
@RestController
@RequestMapping("/position")
@Api(tags = "持仓 待完善")
@Slf4j
public class PositionController {
    /**
     * 服务对象
     */
    @Autowired
    private PositionService positionService;

    @Autowired
    private OperatorFactory factory;
    /**
     * 分页查询
     *
     * @return 查询结果
     */
    @GetMapping
    @ApiOperation(value = "查询所有持仓")
    public BaseResult<PositionVO> queryByPage() {
        return BaseResult.success(this.positionService.queryAll());
    }

    @GetMapping("/getAllType")
    @ApiOperation("获取基金类型")
    public BaseResult getAllType(){
        List<Map<String,  Object>> list = new ArrayList<>();
        for (FundEnum value : FundEnum.values()) {
            Map<String, Object> map = new HashMap<>();
            map.put("id", value.getCode());
            map.put("name", value);
            map.put("desc", value.getDescription());
            list.add(map);
        }
        return BaseResult.success(list);
    }
    @GetMapping("/getFundsByType")
    @ApiOperation("根据类型获取基金列表")
    public BaseResult getFundsByType(@RequestParam("fundEnum") FundEnum fundEnum){
        return BaseResult.success(FundUtils.getFunds(fundEnum));
    }


    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    @ApiOperation(value = "通过主键查询单条数据")
    public BaseResult<PositionVO>  queryById(@PathVariable("id") Integer id) {
        return BaseResult.success(positionService.queryById(id));
    }

    /**
     * 新增持仓
     *
     * @param position 实体
     * @return
     */
    @PostMapping("/add")
    @ApiOperation(value = "新增持仓")
    public BaseResult addPosition(@RequestBody Position position) {
        log.info("param={}",position);
         positionService.addPosition(position);
         return BaseResult.success(Boolean.TRUE);
    }
    @PostMapping("/quickAdd")
    @ApiOperation(value = "快速添加")
    public BaseResult quickAdd(@RequestBody Position position) {
        positionService.quickAdd(position);
        return BaseResult.success(Boolean.TRUE);
    }

    /**
     * 编辑数据
     *
     * @param position 实体
     * @return 编辑结果
     */
    @PutMapping("/edit")
    @ApiOperation(value = "编辑数据")
    public BaseResult edit(@RequestBody Position position) {
        positionService.update(position);
        return BaseResult.success(Boolean.TRUE);
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @ApiOperation(value = "删除")
    @DeleteMapping
    public BaseResult<Boolean> deleteById(@RequestParam("id")Integer id) {
        return BaseResult.success(this.positionService.deleteById(id));
    }

    @PostMapping("/transaction")
    @ApiOperation("交易")
    public BaseResult transaction(@RequestBody TransactionRecordVO record){
        return factory.doOperator(record);
    }

}

